package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojian.pick.entity.BondHistory;
import com.xiaojian.pick.mapper.BondHistoryMapper;
import com.xiaojian.pick.service.BondHistoryService;
import com.xiaojian.pick.util.ExcelUtils;
import com.xiaojian.pick.vo.HistoryVO;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author whycode
 * @title: BondHistoryServiceImpl
 * @projectName campus-secondary-market-app---sys
 * @description: TODO
 * @date 2022/3/1315:44
 */

@Service
public class BondHistoryServiceImpl extends ServiceImpl<BondHistoryMapper, BondHistory> implements BondHistoryService {
    @Override
    public int importFromExcel() {

        List<BondHistory> historyList = ExcelUtils.getWorkbook("F:\\关照闲置物品结缘表0311.xlsx");

        boolean batch = this.saveBatch(historyList);

        return 0;
    }

    @Override
    public List<HistoryVO> listByPage(Integer page, Integer limit) {
        int start = (page - 1) * limit;

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        List<String> list = getBaseMapper().queryDate(start, limit);
        QueryWrapper<BondHistory> wrapper = new QueryWrapper<>();
        wrapper.in("DATE(create_time)",list);
        List<BondHistory> records = this.list(wrapper);

        Map<String, List<BondHistory>> listMap = records.stream().map(bondHistory -> {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            bondHistory.setDate(format.format(bondHistory.getCreateTime()));
            return bondHistory;
        }).collect(Collectors.groupingBy(BondHistory::getDate));

        List<String> strings = new ArrayList<>(listMap.keySet());

        strings.sort((o1, o2) -> -o1.compareTo(o2));

        List<HistoryVO> result = new ArrayList<>();
        strings.forEach(s -> {
            result.add(new HistoryVO().setDate(s).setList(listMap.get(s)));
        });
        return result;
    }




}
